package com.scm.ingestion;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.log4j.Logger;

import scm.classifica.jaxb.Team;
import scm.live.jaxb.CMPBean;
import scm.live.jaxb.MATCHTBean;
import scm.quote.jaxb.Match;

import com.scm.property.IPropertyIDCamp;
import com.scm.property.PropertyConfig;
import com.scm.property.IPropertyConfig;
import com.scm.property.PropertyIDCamp;

public class IngestionImpl implements Ingestion{
	
	private static Logger log = Logger.getLogger(IngestionImpl.class);
	IPropertyConfig property =  null;
	
	public IngestionImpl(){
	     property = new PropertyConfig();
		
	}
	

	public void startIngestionIDCamp(String ID,int position) {
		String urlClass = property.getStringIngestionUrl("addIdCmp.php");
		IPropertyIDCamp legenda = new PropertyIDCamp();
		Map<String, String> param = new HashMap<String, String>();
		param.put("NAME_CMP", (legenda.getStringName(ID).trim()).replace(" ","_"));
		param.put("ID_CMP", ID);
		param.put("PST",String.valueOf(position));
		callHttp(param,urlClass);	
		
	}
	

	public void startDeleteQuote() {
		log.info("Delete Quote");
		String urlClass = property.getStringIngestionUrl("deleteQuote.php");
		Map<String, String> param = new HashMap<String, String>();
		callHttp(param,urlClass);	
	}
	

	public void startDeleteClass() {
		log.info("Delete Classifiche");
		String urlClass = property.getStringIngestionUrl("deleteClass.php");
		Map<String, String> param = new HashMap<String, String>();
		callHttp(param,urlClass);	
	}
	

	public void startDeleteIdCmp() {
		log.info("Delete Id Campionati");
		String urlClass = property.getStringIngestionUrl("deleteIdCmp.php");
		Map<String, String> param = new HashMap<String, String>();
		callHttp(param,urlClass);	
	}
	

	public void startDeleteLive() {
		log.info("Delete Live");
		String urlClass = property.getStringIngestionUrl("deleteLive.php");
		Map<String, String> param = new HashMap<String, String>();
		callHttp(param,urlClass);
	}
	

	public void startDeleteCmpLive() {
		log.info("Delete Campionati Live");
		String urlClass = property.getStringIngestionUrl("deleteCmpLive.php");
		Map<String, String> param = new HashMap<String, String>();
		callHttp(param,urlClass);
	}

	public void startIngestionClass(Team team) {
		String urlClass = property.getStringIngestionUrl("addClass.php");
		Map<String, String> param = new HashMap<String, String>();
		param.put("CMP", team.getCmp().replace(" ","_"));
		param.put("TEAM", (team.getNameTeam().trim()).replace(" ","_"));
		param.put("PT", team.getTotalResult().getPuntiTotali());
		param.put("PG", team.getTotalResult().getPartiteGiocate());
		param.put("PV", team.getTotalResult().getVinte());
		param.put("PN", team.getTotalResult().getNulle());
		param.put("PP", team.getTotalResult().getPerse());
		param.put("RF", team.getTotalResult().getRfatti());
		param.put("RS", team.getTotalResult().getRsubite());
		
		param.put("PVH", team.getHomeResult().getVinte());
		param.put("PNH", team.getHomeResult().getNulle());
		param.put("PPH", team.getHomeResult().getPerse());
		param.put("RFH", team.getHomeResult().getRfatti());
		param.put("RSH", team.getHomeResult().getRsubite());
		
		param.put("PVO", team.getOutDoorResult().getVinte());
		param.put("PNO", team.getOutDoorResult().getNulle());
		param.put("PPO", team.getOutDoorResult().getPerse());
		param.put("RFO", team.getOutDoorResult().getRfatti());
		param.put("RSO", team.getOutDoorResult().getRsubite());
		
		callHttp(param,urlClass);	
	}
	

	public void startIngestionLive(MATCHTBean match) {
		String urlLive = property.getStringIngestionUrl("addLiveMatch.php");
		Map<String, String> param = new HashMap<String, String>();
	
		param.put("CMP",match.getCMP().trim());
		param.put("SQA",(match.getSQ1().trim()).replace(" ","_"));
		param.put("SQB", (match.getSQ2().trim()).replace(" ","_"));
		param.put("SCR", (match.getSCORE().trim()).replace(" ",""));
		param.put("TIME", (match.getSTATUS().trim()).replace("'","").replace(" ",""));
		callHttp(param,urlLive);	
	}
	

	public void startIngestionCompetation(CMPBean cmp) {
		String urlCompetation = property	.getStringIngestionUrl("addCmpMatch.php");
		Map<String, String> param = new HashMap<String, String>();
		param.put("CMP",cmp.getID().trim());
		param.put("NAME", (cmp.getNAMECOMPETATION().trim()).replace(" ","_"));
		callHttp(param,urlCompetation);	
	}


	public void startIngestionQuote(Match match) {		
		String urlQuote = property.getStringIngestionUrl("addQuote.php");
		Map<String, String> param = new HashMap<String, String>();
	
		param.put("DT", match.getDataMatch().replace(" ","_"));
		param.put("MTH", match.getSquadraGuest().replace(" ","_"));
		
		param.put("AVN", match.getAvvenimento());
		param.put("PLS", match.getPalinsesto());
		param.put("CMP", match.getTipoAvv());
		
		param.put("R1F", match.getRisultatoFinale().getR1());
		param.put("RXF", match.getRisultatoFinale().getRx());
		param.put("R2F", match.getRisultatoFinale().getR2());
		
		param.put("R1D", match.getDoppiaChance().getR1());
		param.put("RXD", match.getDoppiaChance().getRx());
		param.put("R2D", match.getDoppiaChance().getR2());
		
		param.put("R1T", match.getRisultato1Tempo().getR1());
		param.put("RXT", match.getRisultato1Tempo().getRx());
		param.put("R2T", match.getRisultato1Tempo().getR2());
		
		param.put("R1H", match.getHandicap().getR1());
		param.put("RXH", match.getHandicap().getRx());
		param.put("R2H", match.getHandicap().getR2());
		
		param.put("UO15U", match.getUO15().getRu());
		param.put("UO15O", match.getUO15().getRo());
		
		param.put("UO25U", match.getUO25().getRu());
		param.put("UO25O", match.getUO25().getRo());
		
		param.put("UO35U", match.getUO35().getRu());
		param.put("UO35O", match.getUO35().getRo());
		
		param.put("GL", match.getGNG().getG());
		param.put("NGL", match.getGNG().getNg());
		
		param.put("S1G", match.getSegna().getS1G());
		param.put("S1NG", match.getSegna().getS1Ng());
		
		param.put("S2G", match.getSegna().getS2G());
		param.put("S2NG", match.getSegna().getS2Ng());
		
		callHttp(param,urlQuote);	
	
	} 
	
	
	private String callHttp(Map<String, String> map,String urlQuote) {
		HttpClient client = new HttpClient();
		PostMethod method = new PostMethod(urlQuote);
		log.debug("REQUEST: "+urlQuote);
		Iterator iterator = map.entrySet().iterator();
		while (iterator.hasNext()) {
			Entry<String, String> mapEntry = (Entry<String, String>) iterator.next();
			method.setParameter(mapEntry.getKey(),mapEntry.getValue());
		}
		
		method.setParameter("key",ProtocolSecret.getSecretId(client));
		String response = null;
		try {
			client.executeMethod(method);
			response = method.getResponseBodyAsString();
			log.debug("RESPONSE: "+response);
			log.debug("->"+method.getStatusCode());
			method.releaseConnection();
		} catch (HttpException e) {
			log.error("HttpException:",e);
		} catch (IOException e) {
			log.error("IOException:",e);
		}
		return response;
	}




}
